package com.pro.exam.web.listener;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.ServletContext;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.context.ServletContextAware;

import com.pro.exam.common.utils.DataUtils;
import com.pro.exam.web.model.TransDictPojo;
import com.pro.exam.web.service.DictTransService;

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class ApplicationStart implements InitializingBean, ServletContextAware {

	/*@Resource
	private DictTransService dictTransService;*/
	@Resource(name="redisTemplate")
	private RedisTemplate<String, String> redisTemplate;
	/**
	 * spring项目启动时加载redis中的数据
	 * 初始化DataUtils数据
	 */
	@Override
	public void setServletContext(ServletContext servletContext) {
		Map<String, HashMap<String, String>> returnMap = new HashMap<String,HashMap<String,String>>();
		Set<String> keys=redisTemplate.opsForSet().members("trans");
		Iterator<String> it = keys.iterator();
		while (it.hasNext()) {
			HashMap<String, String> transMap = new HashMap<String, String>();
			String key = (String) it.next();
			key = key.replaceAll("\"", "");
			String ch1=redisTemplate.opsForHash().get(key, "ch")==null?null:redisTemplate.opsForHash().get(key, "ch").toString();
			String en1=redisTemplate.opsForHash().get(key, "en")==null?null:redisTemplate.opsForHash().get(key, "en").toString();
			String jp1=redisTemplate.opsForHash().get(key, "jp")==null?null:redisTemplate.opsForHash().get(key, "jp").toString();
			transMap.put("ch", ch1);
			transMap.put("en", en1);
			transMap.put("jp", jp1);
			returnMap.put(key, transMap);
		}
		DataUtils.ANALYZER_WORDS = returnMap;
		DataUtils.SWITCH_INITIAL_WORDS_LIB = true;
		/*
		List<TransDictPojo> list = dictTransService.selectAll();
		for (int i = 0; i < list.size(); i++) {
			TransDictPojo pojo = new TransDictPojo();
			pojo = list.get(i);
			String ch = pojo.getCh();
			Map<String, String> map = pojo.getTranslates();
			HashMap<String, String> map1 = new HashMap<String, String>();
			map1.put("ch", ch);
			map1.put("en", map.get("en"));
			map1.put("jp", map.get("jp"));
			DataUtils.ANALYZER_WORDS.put(ch, map1);
		}*/
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		// TODO Auto-generated method stub
		
	}
/*
	@Autowired
	TransDictMapper transDictMapper;
	
	@Override
	public void contextInitialized(ServletContextEvent sce) {
		try { 
			System.out.println("-----------------------just do it-----------------------");
			List<TransDictPojo> list = transDictMapper.selectAll();
			for (int i = 0; i < list.size(); i++) {
				TransDictPojo pojo = new TransDictPojo();
				pojo = list.get(i);
				String ch = pojo.getCh();
				HashMap<String, String> map = (HashMap<String, String>)pojo.getTranslates();
				
				DataUtils.ANALYZER_WORDS.put(ch, map);
			}
		} catch (Throwable e) {  
            e.printStackTrace();              
        }  
		
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		// TODO Auto-generated method stub
		
	}*/
	
}
